/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package com.north.king.profile.http.client.resttemplate.server.remote;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.north.king.profile.http.client.resttemplate.common.ConstantVariable;
import com.north.king.profile.http.client.resttemplate.domain.WorkItem;
import com.north.king.profile.http.client.resttemplate.server.data.CreateConditionData;
import com.north.king.profile.http.client.resttemplate.server.data.CreateWorkItemData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * class info
 *
 * @author hujia
 * @date 2018/11/5 21:21
 */
@Service
@Slf4j
public class RedisClient {

    @Value("${profile.remote.url}")
    private String baseUrl;

    private final CreateWorkItemData createWorkItemData;
    private final RestTemplate template;
    private final ObjectMapper jackson;

    @Autowired
    public RedisClient(CreateWorkItemData createWorkItemData, RestTemplate template,
                       ObjectMapper jackson) {
        this.template = template;
        this.createWorkItemData = createWorkItemData;
        this.jackson = jackson;
    }

    private HttpEntity createHttpEntity(String jsonString) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity formEntity = new HttpEntity(jsonString, headers);
        return formEntity;
    }

    public void begin(String title) {
        template.getForObject(baseUrl + ConstantVariable.URL_BEGIN + title, String.class);
    }

    public void end() {
        template.getForObject(baseUrl + ConstantVariable.URL_END, String.class);
    }

    public void listPush(int number, List<Integer> range) throws Exception {
        StopWatch watch = new StopWatch("list-push");
        watch.start("create-data");
        List<WorkItem> dataList;
        if (null == range) {
            dataList = createWorkItemData.workItemDataRandom(number, 10);
        } else {
            dataList = createWorkItemData.workItemDataDesignated(number, range);
        }
        String jsonString = jackson.writeValueAsString(dataList);
        HttpEntity formEntity = createHttpEntity(jsonString);
        watch.stop();
        watch.start("client-request");
        ResponseEntity<String> resultPut = template.postForEntity(baseUrl + "/queue/add-list", formEntity, String.class, jsonString);
        watch.stop();
        log.info(watch.prettyPrint());
    }

    public String recommendedByPerson(Map<String, String> person) throws Exception {
        String jsonString = jackson.writeValueAsString(person);
        HttpEntity formEntity = createHttpEntity(jsonString);
        ResponseEntity<String> resultPut = template.postForEntity(baseUrl + "/queue/recommended", formEntity, String.class, jsonString);
        return resultPut.getBody();
    }


}
